<template>
  <div>
    <div class="mod-config">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <!--<el-form-item prop="sharePageName">-->
        <!--<el-input v-model="datas.sharePageName" placeholder="页面名称"></el-input>-->
        <!--</el-form-item>-->
        <el-form-item>
          <el-select v-model="datas.shareType" clearable placeholder="请选择">
            <el-option
              v-for="item in dateSelect"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="success" icon="el-icon-search" @click="search()">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="isAuth('operate:bannerinfo:save')"
            type="primary"
            @click="addOrUpdateHandle(0)"
          >新增
          </el-button>
          <!--<el-button-->
          <!--v-if="isAuth('operate:bannerinfo:delete')"-->
          <!--type="danger"-->
          <!--@click="deleteHandle()"-->
          <!--:disabled="dataListSelections.length <= 0"-->
          <!--&gt;批量删除-->
          <!--</el-button>-->
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%"
      >
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="shareTypetext" header-align="center" align="center" label="分享类型"></el-table-column>
        <el-table-column prop="sharePageInfo" header-align="center" align="center" label="邀请卡文案"></el-table-column>
        <el-table-column prop="shareImg" header-align="center" align="center" label="分享图标">
          <template slot-scope="scope">
            <img v-if="scope.row.shareImg" :src="scope.row.shareImg" style="width:100px;height:40px;">
            <span v-else>——</span>
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="分享标题">
          <template slot-scope="scope">
            <span>{{scope.row.shareTitle || '——'}}</span>
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="分享文案">
          <template slot-scope="scope">
            <span>{{scope.row.shareDesc || '——'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="isAlive" header-align="center" align="center" label="分享页状态" v-if="false">
          <template slot-scope="scope">
            <!-- <div>{{scope.row.isAlive==1?'启用':''}}</div>
            <div class="active">{{ scope.row.isAlive === 0 ? '禁用': '' }}</div> -->
            <el-switch v-model="scope.row.isInUse"
                       :active-value="1"
                       :inactive-value="0"
                       disabled
                       @change="changeSwitch(scope.row.id)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="banner(scope.row.id)">查看</el-button>
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <!--<el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
      <!-- 查看 -->
      <!-- <banner-see v-if="bannersees" ref="seebanner" @refreshDataList="getDataList"></banner-see> -->
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <share-manage-detail v-if="shareManageDetail" ref="shareManageDetail" @refreshDataList="getDataList"></share-manage-detail>
    </div>
    <div class="mod-config" v-if="false">
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;"
      >
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="gmtCreated" header-align="center" align="center" label="分享图标"></el-table-column>
        <el-table-column prop="bannerLink" header-align="center" align="center" label="分享类型"></el-table-column>
        <el-table-column prop="isAlive" header-align="center" align="center" label="邀请卡文案">
          <template slot-scope="scope">
            <!-- <div>{{scope.row.isAlive==1?'启用':''}}</div>
            <div class="active">{{ scope.row.isAlive === 0 ? '禁用': '' }}</div> -->
            <el-switch v-model="scope.row.isAlive"
                       :active-value="1"
                       :inactive-value="0"
                       @change="changeSwitch(scope.row.id)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="bannerUrl" header-align="center" align="center" label="页面名称">
          <template slot-scope="scope">
            <img :src="scope.row.bannerUrl" style="width:100px;height:40px;">
          </template>
        </el-table-column>
        <el-table-column prop="gmtUpdate" header-align="center" align="center" label="分享标题"></el-table-column>
        <el-table-column prop="bannerType" header-align="center" align="center" label="分享文案"></el-table-column>
        <el-table-column prop="bannerType" header-align="center" align="center" label="分享页状态"></el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <!-- <el-button type="text" size="small" @click="banner(scope.row.id)">查看</el-button> -->
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>

            <!--<el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
          </template>
        </el-table-column>
      </el-table>

    </div>
  </div>
</template>
<style>
  .active {
    color: #f00;
  }
</style>
<script>
  import AddOrUpdate from './shareManage-add-or-update'
  import ShareManageDetail from './shareManage-detail'
  // import BannerSee from "./banner-see"
  export default {
    data() {
      return {
        dataForm: {
          key: ''
        },
        datas: {
          sharePageName: '',
          shareType: '全部'
        },
        dateSelect: [
          {
            label: '邀请好友',
            value: 1
          }, {
            label: '书籍分享',
            value: 2
          }, {
            label: '公益活动分享',
            value: 3
          }, {
            label: '知识彩蛋分享',
            value: 4
          }, {
            label: '伴学记录分享',
            value: 5
          }, {
            label: '学习记录分享',
            value: 6
          }
        ],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        shareManageDetail: false
        // bannersees:false,
      }
    },
    components: {
      AddOrUpdate, ShareManageDetail
    },
    activated() {
      this.getDataList({
        page: this.pageIndex,
        limit: this.pageSize,
        key: this.dataForm.key
      })
    },
    methods: {
      // 获取数据列表
      getDataList(data) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/operate/shareInfo/list'),
          method: 'post',
          params: this.$http.adornParams(data)
        }).then(({data}) => {
          if (data && data.code === 0) {
            data.page.list.forEach(item => {
              switch (item.shareType) {
                case 1:
                  item.shareTypetext = '邀请好友'
                  break
                case 2:
                  item.shareTypetext = '书籍分享'
                  break
                case 3:
                  item.shareTypetext = '公益活动分享'
                  break
                case 4:
                  item.shareTypetext = '知识彩蛋分享'
                  break
                case 5:
                  item.shareTypetext = '伴学记录分享'
                  break
                case 6:
                  item.shareTypetext = '学习记录分享'
                  break
                case 7:
                  item.shareTypetext = '笔记分享'
                  break
                case 8:
                  item.shareTypetext = '个人助力海报'
                  break
                case 9:
                  item.shareTypetext = '团队助力海报'
                  break
                case 10:
                  item.shareTypetext = '学习成就卡分享'
                  break
                // default:
                //   默认代码块
              }
            })
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增/修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.iskans = true
          this.$refs.addOrUpdate.title = id === 0 ? '分享管理新增' : '分享管理修改'
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 禁用
      changeSwitch(id) {
        this.dataForm.id = id
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/operate/bannerinfo/disable/${this.dataForm.id}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data.code === 500) {
              this.$message.error(data.msg)
            }
            this.getDataList()
          })
        }
      },
      // 查看
      // banner(id){
      //   this.bannersees = true
      //   this.$nextTick(() => {
      //     this.$refs.seebanner.init(id)
      //   })
      // },
      // 删除
      deleteHandle(id) {
        var ids = id
          ? [id]
          : this.dataListSelections.map(item => {
            return item.id
          })
        this.$confirm(
          `确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`,
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          this.$http({
            url: this.$http.adornUrl('/operate/shareInfo/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      banner: function (id) {
        this.shareManageDetail = true
        this.$nextTick(() => {
          this.$refs.shareManageDetail.title = '分享管理查看'
          this.$refs.shareManageDetail.init(id)
        })
      },
      search: function () {
        this.pageIndex = 1
        this.getDataList({
          // sharePageName: this.datas.sharePageName,
          shareType: this.datas.shareType === '全部' ? undefined : this.datas.shareType,
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key
        })
      }
    }
  }
</script>
